<?php
session_start();
session_register('queue');
$library = $_GET['library'];
$id = $_GET['id'];

$config = sqlite_open("config.db");
$gen = @sqlite_fetch_array(@sqlite_query($config, "SELECT * FROM general"));
$lib = sqlite_fetch_array(sqlite_query($config, "SELECT * FROM library WHERE id='$library'"));
?>
<html>
<head>
<style type="text/css">
a {color: <?php echo $gen['fncolor']?>}
td {font-size: 14}
</style>
</head>
<body bgcolor="<?php echo $gen['bgcolor']?>" style="font-family: Verdana; color: <?php echo $gen['fncolor']?>;">
<?

$db = sqlite_open($gen['path'] . "\\library\\". $lib['file']);

if ($_GET['action'] == "delete") {
  if ($id != NULL) {
    if ($id != $_SESSION['playing']) {
      unset($_SESSION['queue'][$id]);
      $n = $id + 1;
      $c = (count($_SESSION['queue']));
      for ($i = $id; $i < $c; $i++) {
        $_SESSION['queue'][$i] = $_SESSION['queue'][$n];
        $n++;
      }
      unset($_SESSION['queue'][$c]);
      if ($_SESSION['playing'] > $id) $_SESSION['playing'] = ($_SESSION['playing'] - 1);
    }
  }
}


$sql_tags = sqlite_query($db, "SELECT * FROM tags");
while ($tags = sqlite_fetch_array($sql_tags)) {
  if ($tags['tag_name'] == "Genre")     $tag_genre = $tags['tag_col'];
  if ($tags['tag_name'] == "Album")     $tag_album = $tags['tag_col'];
  if ($tags['tag_name'] == "Artist")    $tag_artist = $tags['tag_col'];
  if ($tags['tag_name'] == "Track")     $tag_track = $tags['tag_col'];
}

echo "<a href=\"queue.php?library=$library\">Refresh</a><br><br>";

$t = count($_SESSION['queue']);
for ($i=0; $i < $t; $i++) {
  $id = $_SESSION['queue'][$i];
  $data = sqlite_fetch_array(sqlite_query($db, "SELECT * FROM items WHERE item_id='$id'"));
  if ($i == 0) $go = ($t-1);
  elseif ($i == 1) $go = "ss";
  else $go = ($i - 1);
  echo "<a href=\"queue.php?library=$library&action=delete&id=$i\">X</a>&nbsp;&nbsp;&nbsp;&nbsp;";
  if ($_SESSION['playing'] == $i) echo "<b>";
  else echo "<a href=\"player.php?library=$library&go=$go&type=music\" target=\"Music\">";
  echo $data[$tag_artist]." - ".$data['item_name']."<br>";
  if ($_SESSION['playing'] == $i) echo "</b>";
  else echo "</a>";
}
?>
</body>
</html>